﻿using UnityEngine;
using System.Collections;

public class SelectionControl : MonoBehaviour {
	public GameObject cam;
	public Player localPlayer;
	private SelectionGUI gui;
	private bool selecting = false;
	// Use this for initialization
	void Start () {
		gui=(SelectionGUI)cam.GetComponent("SelectionGUI");
	}
	
	// Update is called once per frame
	void Update () {
	 	if(Input.GetButtonDown("Fire1")){
			if(Input.mousePosition.x>5 && Input.mousePosition.x-5<128 &&
			   Input.mousePosition.y>5 && Input.mousePosition.y-5<128){
				float x = Input.mousePosition.x-5, y = Input.mousePosition.y-5;
				x/=128;
				y/=128;
				x-=.5f;
				x*=200;
				y-=.5f;
				y*=200;
				//some x,y
				Camera.main.GetComponent<CameraController>().moveToLocation(x, y);
			}
			else{
				gui.setStart(Input.mousePosition.x/Screen.width,Input.mousePosition.y/Screen.height);
				gui.setDisplay(true);
				selecting=true;
			}
		}
		if(selecting && Input.GetButton("Fire1") ){
			gui.setEnd(Input.mousePosition.x/Screen.width,Input.mousePosition.y/Screen.height);
		}
		if(selecting && Input.GetButtonUp("Fire1")){
			selecting=false;
			gui.setDisplay(false);
			processSelection();
		}

		if (Input.GetAxis("Mouse ScrollWheel") < 0) // back
		{
			Camera.main.orthographicSize = Mathf.Max(Camera.main.orthographicSize-1, 5);
			
		}
		if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward
		{
			Camera.main.orthographicSize = Mathf.Min(Camera.main.orthographicSize+1, 28);
		}
		if(Input.GetButtonDown("Fire2")){
			if(Input.mousePosition.x>5 && Input.mousePosition.x-5<128 &&
			   Input.mousePosition.y>5 && Input.mousePosition.y-5<128){
				float x = Input.mousePosition.x-5, y = Input.mousePosition.y-5;
				x/=120;
				y/=120;
				x-=.5f;
				x*=200;
				y-=.5f;
				y*=200;
				orderUnits(new Vector3(x,0,y));
			}
			else{
				RaycastHit hitinfo;
				int layer = 1 << LayerMask.NameToLayer("Terrain");
				if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitinfo,
					float.MaxValue,layer)){
					orderUnits(hitinfo.point);
				}
			}
		}
	}
	void orderUnits(Vector3 point){
		Group g = new Group();
		//ArrayList existingGroups = new ArrayList();
		//ArrayList existingGroupCount = new ArrayList();
		GroupManager man=(GroupManager)GameObject.Find("Managers").GetComponent("GroupManager");
		g.target=point;
		/*
		foreach(Unit u in localPlayer.getSelectedUnits()){
			if(u.myGroup!=null){
				if(!existingGroups.Contains(u)){
					existingGroups.Add(u.myGroup);
					existingGroupCount.Add(1);
				}
				else{
					int ind = existingGroups.IndexOf(u.myGroup);
					existingGroupCount[ind]= ((int)existingGroupCount[ind])+1;
				}
			}
		}
		if(existingGroups.Count!=0){
			int max=0;
			for(int i=1;i<existingGroupCount.Count;i++){
				if((int)existingGroupCount[i]>(int)existingGroupCount[max])
					max=i;
			}
			if(((Group)existingGroups[max]).myUnits.Count-(int)existingGroupCount[max]<10){
				Group maxG = (Group)existingGroups[max];
				for(int i=0;i<maxG.myUnits.Count;){
					if(localPlayer.getSelectedUnits().Contains(maxG.myUnits[i])){
						i++;
					}
					else{
						g.myUnits.Add(maxG.myUnits[i]);
						((Unit)maxG.myUnits[i]).myGroup=g;
						maxG.myUnits.RemoveAt(i);
					}
				}
				foreach(Unit u in localPlayer.getSelectedUnits()){
					if(!maxG.myUnits.Contains(u))
					{
						maxG.myUnits.Add(u);
						u.myGroup=maxG;
					}
				}
				g.target=maxG.target;
				maxG.target=point;
				man.groups.Add(g);
				return;
			}
		}*/
		foreach(Unit u in localPlayer.getSelectedUnits()){
			g.myUnits.Add(u);
			u.myGroup=g;
		}
		man.groups.Add(g);
		/*
		foreach(Unit u in localPlayer.getSelectedUnits()){
			u.target(point);	
		}*/
	}
	bool testUnitOwnership(GameObject o){
		if(o.transform.parent== null)return false;
		if(o.transform.parent.GetComponent("Unit")==null) return false;
		return(((Unit)o.transform.parent.GetComponent("Unit")).owner==localPlayer);
	}
	Unit getUnit(GameObject o){
		return((Unit)o.transform.parent.GetComponent("Unit"));
	}
	void processSelection(){
		Vector3 s = gui.getStart(), e = gui.getEnd();
		s.x*=Screen.width;
		s.y*=Screen.height;
		e.x*=Screen.width;
		e.y*=Screen.height;
		Vector3 tst = e-s;
		if(tst.magnitude<5){
			RaycastHit hitinfo;
			int layer = 1 << LayerMask.NameToLayer("Units");
			if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitinfo,
				float.MaxValue,layer) && testUnitOwnership(hitinfo.collider.gameObject)){
				localPlayer.clearSelection();
				localPlayer.getSelectedUnits().Add(getUnit(hitinfo.collider.gameObject));
				getUnit(hitinfo.collider.gameObject).displaySelection(true);
			}
		}
		else{
			Rect r= new Rect(Mathf.Min(s.x,e.x),Mathf.Min(s.y,e.y),Mathf.Max(s.x-e.x,e.x-s.x),Mathf.Max(s.y-e.y,e.y-s.y));
			ArrayList units = localPlayer.getUnits();
			ArrayList selectedUnits = localPlayer.getSelectedUnits();
			localPlayer.clearSelection();
			foreach(Unit u in units)if(u.type!=UnitType.Building){
				Vector3 coords = u.getScreenCoords();
				if(r.Contains(coords)){
					selectedUnits.Add(u);
					u.displaySelection(true);
				}
			}
		}
	}
}
